package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.CodeUtil;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.app.mapper.UserInfoMapper;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.service.SmsService;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.atguigu.lease.web.app.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.lettuce.core.ScriptOutputType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private SmsService smsService;
    @Autowired
    private StringRedisTemplate redisTemplate;
@Autowired
private UserInfoMapper userInfoMapper;

    @Override
    public void   getCode(String phone) {
        //生成短信验证码
        String code = CodeUtil.createCode(6);
        //生成标准的key
        String key =RedisConstant.APP_LOGIN_PREFIX+phone;

        //判断验证码是否发送频繁 一分钟之内不允许发送
        Boolean b = redisTemplate.hasKey(key);
        if (b) {
            Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS);

            if (RedisConstant.APP_LOGIN_CODE_TTL_SEC - expire < RedisConstant.APP_LOGIN_CODE_RESEND_TIME_SEC){

                throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_TOO_OFTEN);
            }

        }

        //发送短信
        smsService.sendCode(phone, code);
        //将验证码保存在redis中
        redisTemplate.opsForValue().set(key, code, RedisConstant.APP_LOGIN_CODE_TTL_SEC, TimeUnit.SECONDS);




    }

    @Override
    public String login(LoginVo loginVo) {
        //判断密码是否为空
        if (loginVo.getPhone() == null || loginVo.getPhone().trim().equals("")){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);

        }
        //判断验证码是否为空
        if (loginVo.getCode() == null || loginVo.getCode().trim().equals("")){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        //获取验证码
        String key=RedisConstant.APP_LOGIN_PREFIX+loginVo.getPhone();
        String code = redisTemplate.opsForValue().get(key);
        //判断验证码
        if(code == null){
            throw new LeaseException(ResultCodeEnum.TOKEN_EXPIRED);
        }
        if (!code.equals(loginVo.getCode())){

            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getPhone, loginVo.getPhone());
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        //判断用户是否为空 如果为空当注册处理
        if (userInfo == null){
            userInfo = new UserInfo();
             userInfo.setPhone(loginVo.getPhone());
             userInfo.setStatus(BaseStatus.ENABLE);
             userInfo.setNickname("用户-"+loginVo.getPhone().substring(7));
             userInfoMapper.insert(userInfo);
        }else {
        //判断是否禁用
        if (userInfo.getStatus() == BaseStatus.DISABLE) {
            throw new LeaseException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
        }

        }
        return JwtUtil.createToken(userInfo.getId(),loginVo.getPhone());

    }

    @Override
    public UserInfoVo info(String phone) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getPhone, phone);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        UserInfoVo userInfoVo = new UserInfoVo(userInfo.getNickname(), userInfo.getAvatarUrl());
        return userInfoVo;
    }
}
